﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.IO;

namespace Helper
{
    public class XmlTools
    {
        /// <summary>
        /// Убрать NameSpace Xml файла 
        /// </summary>
        /// <param name="d"></param>
        /// <returns></returns>
        public static XmlDocument GetXmlNoNamespaceDocument(XmlDocument d)
        {
            if (d != null)
            {
                XmlDocument doc = null;
                MemoryStream streamRead = null;
                MemoryStream streamWrite = null;
                XmlTextWriter writer = null;
                XmlTextReader reader = null;

                try
                {
                    streamRead = new MemoryStream();
                    d.Save(streamRead);

                    streamWrite = new MemoryStream();
                    writer = new XmlTextWriter(streamWrite, System.Text.Encoding.UTF8);

                    ////////////
                    streamRead.Position = 0;
                    reader = new XmlTextReader(streamRead);
                    reader.Namespaces = true;
                    reader.WhitespaceHandling = WhitespaceHandling.None;

                    writer.WriteStartDocument();

                    string docElemName = null;
                    bool b = true;
                    while (b)
                    {
                        reader.Read();
                        switch (reader.NodeType)
                        {
                            case XmlNodeType.Attribute:
                                writer.WriteAttributeString(reader.Prefix, reader.LocalName, reader.NamespaceURI, reader.Value);
                                break;
                            case XmlNodeType.CDATA:
                                writer.WriteCData(reader.Value);
                                break;
                            case XmlNodeType.Comment:
                                writer.WriteComment(reader.Value);
                                break;
                            case XmlNodeType.DocumentType:
                                writer.WriteDocType(reader.Name, null, null, null);
                                break;
                            case XmlNodeType.Element:
                                writer.WriteStartElement(null, reader.LocalName, null);

                                if (reader.HasAttributes)
                                {
                                    while (reader.MoveToNextAttribute())
                                    {
                                        if (reader.Name.CompareTo("xmlns") != 0)
                                            writer.WriteAttributeString(reader.Name, reader.Value);
                                    }
                                    reader.MoveToElement();
                                }

                                if (reader.IsEmptyElement)
                                    writer.WriteEndElement();


                                if (docElemName == null)
                                    docElemName = reader.Name;
                                break;
                            case XmlNodeType.EndElement:
                                if (docElemName == reader.Name)
                                    b = false;
                                else
                                    writer.WriteEndElement();
                                break;
                            case XmlNodeType.EntityReference:
                                writer.WriteEntityRef(reader.Name);
                                break;
                            case XmlNodeType.ProcessingInstruction:
                                writer.WriteProcessingInstruction(reader.Name, reader.Value);
                                break;
                            case XmlNodeType.SignificantWhitespace:
                                writer.WriteWhitespace(reader.Value);
                                break;
                            case XmlNodeType.Text:
                                writer.WriteString(reader.Value);
                                break;
                            case XmlNodeType.Whitespace:
                                writer.WriteWhitespace(reader.Value);
                                break;

                            case XmlNodeType.None:
                                b=false ;
                                break;
                        }
                    }

                    writer.WriteEndElement();
                    writer.Flush();

                    writer.WriteEndDocument();

                    streamWrite.Position = 0;

                    doc = new XmlDocument();
                    doc.Load(streamWrite);

                    writer.Flush();
                    writer.Close();
                    reader.Close();


                    return doc;
                }
                catch (Exception er)
                {
                    //throw new Exception(er.Message);
                    return null;
                }
                finally
                {
                    if (streamRead != null)
                    {
                        streamRead.Close();
                    }

                    if (streamWrite != null)
                    {
                        streamWrite.Close();
                    }

                    if (reader != null)
                    {
                        reader.Close();
                    }

                    if (writer != null)
                    {
                        try
                        {
                            writer.Close();
                        }
                        catch {}
                    }
                }
            }
            else
            {
                return null;
            }
        }
    }
}
